package me.lc.superDown.layer;

import me.lc.superDown.R;
import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.scene.GameScene;
import me.lc.superDown.util.AudioHelper;

import android.app.Activity;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.actions.ease.EaseBackInOut;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.utils.ResolutionIndependent;
import com.wiyun.game.WiGame;

/**
 * 游戏完成层
 * 
 * @author Administrator
 * 
 */
public class FinishLayer extends Layer {
	Sprite bg;
	Label timeScore;
	Label coinScore;
	Label totalScore;
	Sprite newRecord;
	Button btn_retry;
	Button btn_return;

	public FinishLayer() {
		bg = Sprite.make(GameSystem.tex_finishBg);
		bg.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2);
		addChild(bg);

		float startY = GameSystem.Screen_Height / 2
				+ GameSystem.tex_finishBg.getHeight() / 2;

		timeScore = Label.make("1000000", 25);
		timeScore.setColor(WYColor3B.make(187, 245, 255));
		timeScore.setPosition(GameSystem.Screen_Width / 2, getNewY(startY, 90));
		addChild(timeScore);

		coinScore = Label.make("1000000", 25);
		coinScore.setColor(WYColor3B.make(187, 245, 255));
		coinScore
				.setPosition(GameSystem.Screen_Width / 2, getNewY(startY, 147));
		addChild(coinScore);

		totalScore = Label.make("1000000", 30);
		totalScore.setColor(WYColor3B.make(200, 0, 0));
		totalScore.setPosition(GameSystem.Screen_Width / 2,
				getNewY(startY, 212));
		addChild(totalScore);

		btn_retry = Button.make(R.drawable.button_tryagain,
				R.drawable.button_tryagain_selected, this,
				"onRetryButtonClicked");
		btn_retry
				.setPosition(GameSystem.Screen_Width / 2, getNewY(startY, 262));
		addChild(btn_retry);

		btn_return = Button.make(R.drawable.button_return,
				R.drawable.button_return_selected, this,
				"onReturnButtonClicked");
		btn_return.setPosition(GameSystem.Screen_Width / 2,
				getNewY(startY, 305));
		addChild(btn_return);

		newRecord = Sprite.make(GameSystem.tex_new_record);
		newRecord.setPosition(GameSystem.Screen_Width / 2
				+ GameSystem.tex_finishBg.getWidth() / 2
				- ResolutionIndependent.resolveDp(13), GameSystem.Screen_Height
				/ 2 - GameSystem.tex_finishBg.getHeight() / 2
				+ ResolutionIndependent.resolveDp(13));
		newRecord.setVisible(false);
		addChild(newRecord);
	}

	public void onRetryButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				GameManager.instance.startNewGame(GameScene.make().layer);
			}
		});
	}

	public void onReturnButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				GameManager.instance.startMainMenu(GameScene.make().layer);
			}
		});
	}

	/**
	 * 设置最终得分
	 * 
	 * @param time
	 * @param coin
	 */
	public void setScore(final int time, final int coin) {
		int t = time * 100;
		int c = coin * 200;
		int f = t + c;
		timeScore.setText(t + "");
		coinScore.setText(c + "");
		totalScore.setText(f + "");

		// 判断是否大于本地最高分，如果大于最高分替换最高分，并显示新记录图标。

		if (f > GameManager.instance.currentHigh) {
			showNewHighTip();
			GameManager.instance.currentHigh = f;
			// 保存新纪录成绩
			Thread saveThread = new Thread() {
				@Override
				public void run() {
					GameManager.instance.saveCurrentHigh();
				}
			};
			saveThread.start();
		} else {
			hideNewHighTip();
		}

		// 判断游戏成就
		Thread thread = new Thread() {
			@Override
			public void run() {
				unlockAchievement(time, coin);
			}
		};
		thread.start();

		// 提交分数
		WiGame.submitScore("dbadeff37e6bc0a6", f, null, true);

	}

	private void unlockAchievement(int time, int coin) {
		// 新手报到
		WiGame.unlockAchievement("e85846aa544cdb8d");

		// 时间成就

		if (time >= 30 && time < 50) {
			WiGame.unlockAchievement("1682ef7bb481cb47");
		} else if (time >= 50 && time < 100) {
			WiGame.unlockAchievement("a5abf0ed169e18ae");
		} else if (time >= 100 && time < 150) {
			WiGame.unlockAchievement("fb4db338b6582c33");
		} else if (time >= 150 && time < 250) {
			WiGame.unlockAchievement("40c562f2c1170513");
		} else if (time >= 250 && time < 500) {
			WiGame.unlockAchievement("48374471c621068d");
		} else if (time >= 500) {
			WiGame.unlockAchievement("c368b3a1dad9afcf");
		}

		// 金币成就
		if (coin >= 10 && coin < 20) {
			WiGame.unlockAchievement("207722c265639fef");
		} else if (coin >= 20 && coin < 40) {
			WiGame.unlockAchievement("496e30bfee59306d");
		} else if (coin >= 40 && coin < 80) {
			WiGame.unlockAchievement("9758ec6a92c9a023");
		} else if (coin >= 80 && coin < 200) {
			WiGame.unlockAchievement("37017306f9370773");
		} else if (coin >= 250) {
			WiGame.unlockAchievement("7bbdf9f81b43600a");
		}
	}

	/**
	 * 隐藏新纪录提示
	 */
	private void hideNewHighTip() {
		newRecord.stopAllActions(true);
		newRecord.setVisible(false);
	}

	/**
	 * 显示新纪录提示
	 */
	private void showNewHighTip() {
		newRecord.stopAllActions(true);
		newRecord.setVisible(true);
		// 开始动画
		IntervalAction bigger = (IntervalAction) ScaleTo.make(0.5f, 0.8f, 1.1f)
				.autoRelease();
		IntervalAction smaller = (IntervalAction) bigger.reverse()
				.autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(
				EaseBackInOut.make(bigger), EaseBackInOut.make(smaller))
				.autoRelease();
		Action repeat = (Action) RepeatForever.make(seq).autoRelease();
		newRecord.runAction(repeat);
	}

	private float getNewY(float startY, float y) {
		float bgHeight = ResolutionIndependent.resolveDp(337);
		return startY - (y / 337) * bgHeight;
	}

}
